home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14241 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  68 lines

  1. Path: garlic.com!usenet
  2. From: Grant Robinson <grant@garlic.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: MFC ASSERT_VALID
  5. Date: Fri, 29 Mar 1996 09:05:33 -0800
  6. Organization: South_Valley_Internet
  7. Message-ID: <315C185D.6B46@garlic.com>
  8. NNTP-Posting-Host: h.assign.garlic.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win16; I)
  13. CC: grant@garlic.com
  14.  
  15. In the following program, why does the indicated ASSERT_VALID fail?  I 
  16. encountered this problem deriving my own container class from the MFC 
  17. CArray template, which uses this method of memory allocation.  The method 
  18. used is not how I'd do things, but it's how Microsoft did, and I don't 
  19. see why it shouldn't work.
  20.  
  21. #include <afx.h>
  22.  
  23. class CAge : public CObject
  24. {
  25. private:
  26.     int    m_years;
  27. public:
  28.     CAge() { m_years = 0; }
  29.     CAge(const int age) { m_years = age; }
  30.     CAge(const CAge& a) { m_years = a.m_years; } // copy ctor
  31.     const CAge& operator=(const CAge& a) // assignment
  32.         { m_years = a.m_years; return *this; }
  33.     BOOL operator==(CAge a)
  34.         { return m_years == a.m_years; }
  35. #if defined(_DEBUG)
  36.     void Dump(CDumpContext& dc) const
  37.     {
  38.         CObject::Dump( dc );
  39.         dc << m_years;
  40.     }
  41.     void CAge::AssertValid() const
  42.     {
  43.         CObject::AssertValid();
  44.         ASSERT(m_years >= 0); 
  45.         ASSERT(m_years < 105);
  46.     }
  47. #endif // defined(_DEBUG)
  48. };
  49.  
  50. void main()
  51. {
  52.     {   // This works...
  53.         CAge* pCAge = new CAge[1];
  54.         pCAge[0] = CAge(21);
  55.         ASSERT(pCAge[0] == CAge(21)); // OK
  56.         ASSERT_VALID(&pCAge[0]); // OK
  57.         delete[] pCAge;
  58.     }
  59.     {   // This doesn't...
  60.         CAge* pCAge = (CAge*) new char[1 * sizeof(CAge)];
  61.         pCAge[0] = CAge(21);
  62.         ASSERT(pCAge[0] == CAge(21)); // OK
  63.         ASSERT_VALID(&pCAge[0]); // AfxIsValidAddress fails:
  64.                                  // "illegal vtable pointer"
  65.         delete[] (char*)pCAge;
  66.     }
  67. }
  68.